package cn.bonoon.controllers.fmm;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

import javax.servlet.http.HttpServletRequest;

import org.batatasframework.common.ParameterReader;
import org.batatasframework.web.RequestParameterReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.bonoon.core.plugins.AccountService;
import cn.bonoon.entities.AccountEntity;
import cn.bonoon.kernel.security.LogonUser;
import cn.bonoon.kernel.util.StringHelper;
import cn.bonoon.kernel.web.controllers.AbstractController;
import cn.bonoon.kernel.web.models.DialogModel;
import cn.bonoon.kernel.web.models.JsonResult;

@Controller
@RequestMapping("s/sys/account")
public class AccountResetpwdController extends AbstractController {

	@Value("${user.password.pattern}")
	private String pattern;
	@Value("${user.password.pattern.exception}")
	private String patternException;
	
	@Autowired
	private AccountService accountService;

	@RequestMapping(value = "!{mid}/resetpwd.do", method = { POST, GET })
	public ModelAndView get(HttpServletRequest request, 
			@PathVariable("mid") String mid, 
			@RequestParam("id") Long id, 
			@RequestParam("gridid") String gridid) {
		DialogModel model = new DialogModel(mid, request);
		model.addForm(id);
		model.addObject("id", id);
		model.addObject("gridid", gridid);
		AccountEntity account = accountService.get(id);
		model.addObject("name", account.getName());
		model.addObject("loginName", account.getLoginName());
		model.addObject("pattern", pattern);
		model.addObject("patternException", patternException);
		return model.execute("sys/account/account-resetpwd");
	}

	private static final int PLOY_INPUT = 0;
	private static final int PLOY_RANDOM = 1;

	@ResponseBody
	@RequestMapping(value = "!{mid}/pwd!save.b", method = POST)
	public final JsonResult updatePwd(HttpServletRequest request) {
		try {
			ParameterReader pReader = RequestParameterReader.valueOf(request);
			//String mid=request.getParameter("mid");
			Long id = pReader.notNullLong("id", "必须选择需要修改密码的用户的ID！");//StringHelper.toLong(request.getParameter("id"));
			Integer ploy = pReader.notNullInteger("ploy", "必须指定密码的修改策略！");// StringHelper.toInteger(request.getParameter("ploy"));
			String pwd = pReader.getString("pwd");
			String repwd=pReader.getString("repwd");
			String email=pReader.getString("email");
			String phone=pReader.getString("phone");
			boolean enforce = pReader.getBoolean("enforce", false);//("enforce"));
			//boolean f = null != enforce && enforce.booleanValue();
			LogonUser user = getUser();
			String sendPwd = "";
			switch (ploy) {
			case PLOY_INPUT:
				sendPwd = accountService.resetPassword(user, id, pwd, repwd, enforce);
				break;
			case PLOY_RANDOM:
				sendPwd = accountService.randomPassword(user, id, enforce);
				break;
			default:
				throw new Exception("没有指定有效的密码生成策略！");
			}
			if (StringHelper.isNotEmpty(email)) {
				// 邮件通知
			}
			if (StringHelper.isNotEmpty(phone)) {
				// 手机通知
			}
			return JsonResult.result(sendPwd);
		} catch (Throwable t) {
			return JsonResult.error(t);
		}
	}
}
